package com.chinhnd.booze.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;

import com.chinhnd.booze.R;
import com.chinhnd.booze.activity.DetailActivity;
import com.chinhnd.booze.model.Category;

public class AdapterCategory extends ArrayAdapter<Category> {

	/** the context contain this adapter */
	private Context mContext;
	private LayoutInflater mLiInflater;

	public AdapterCategory(Context context, List<Category> objects) {
		super(context, -1, objects);
		this.mContext = context;
		mLiInflater = LayoutInflater.from(this.mContext);
	}

	/**
	 * Custom the view
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = mLiInflater.inflate(R.layout.grid_item_category, null);
		Category category = getItem(position);
		Button mBtName = (Button) convertView
				.findViewById(R.id.grid_item_category_bt);
		mBtName.setTag(category);

		mBtName.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Category item = (Category) v.getTag();
				Intent intent = new Intent(mContext, DetailActivity.class);
				intent.putExtra("Item", item);
				mContext.startActivity(intent);
			}
		});

		if (category == null)
			return null;
		mBtName.setText(category.getName());
		return convertView;
	}
}
